﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef EventManager_h__
#define EventManager_h__
#include <memory>
#include <functional>
#include <unordered_map>
#include <list>
#include "Event.h"
#include <typeinfo>

#include "../utility/Singleton.h"

namespace SkyEngine2d
{
	/*!
	 \brief 标准事件监听器的句柄
	 \details
	*/
	class SGE_API EventListenerHandle
	{
		uint64_t		  m_id;
		std::string m_eventGuid;
	public:
		//! 构造
		EventListenerHandle();
	
		//! 构造
		/*!
		\param const std::string & event_guid 事件的标识符
		\return
		*/
		EventListenerHandle(const std::string& event_guid);
	
		//! 获取事件的标识符
		/*!
		\return const std::string&
		*/
		const std::string& getEventGuid()const { return m_eventGuid; }
	
	
		//! 获取事件ID
		/*!
		\return uint64_t
		*/
		uint64_t getEventListenerId() const { return m_id; }
	};
	
	
	/*!
	 \brief 标准事件管理器
	 \details
	*/
	class SGE_API EventManager
		:public Singleton<EventManager>
	{
		SINGLETON_CLASS(EventManager)
	public:
	
		//!事件的触发模式
		enum class TriggerMode
		{
			direct,		/*!< 直接触发. 直接触发模式会在触发事件时立即回调事件处理*/
			queue,		/*!< 队列触发. 队列触发模式会在触发事件时将事件加入事件队列中*/
		};
	
		//! 析构函数
		~EventManager();
	
		//! 添加事件处理
		/*!
		\param const std::function<void Event * e > & listener 事件监听器
		\return EventListenerHandle 事件监听器的句柄
		*/
		template<typename EventType>
		EventListenerHandle addListener(const std::function<void(Event* e)>& listener);
	
		//! 移除事件监听器
		/*!
		\param const EventListenerHandle & handle 事件监听器句柄
		\return void
		*/
		void removeListener(const EventListenerHandle& handle);
	
		//! 触发一个事件
		/*!
		\param const std::shared_ptr<Event>& e 事件指针
		\param TriggerMode mode 触发模式
		\return void
		*/
		void triggerEvent(const std::shared_ptr<Event>& e, TriggerMode mode = TriggerMode::direct);
	
	
	private:
		//! 事件处理
		/*!
		\return void
		*/
		void eventLoop();
	
		//！ 构造
		EventManager();
	
	private:
		//事件类型 -> 监听器映射表
		typedef std::unordered_map<uint64_t, std::function<void(Event* e)>> EventListenerMap;
		std::unordered_map<std::string, std::unique_ptr<EventListenerMap>> m_listeners;
		//事件队列
		std::list<std::shared_ptr<Event>> m_eventQueue;
	
		FRIEND_CLASS(GameWorld)
	};
	
	template<typename EventType>
	EventListenerHandle EventManager::addListener(const std::function<void(Event* e)>& listener)
	{
		const std::string& guid = EventType::getEventGuid();
		EventListenerHandle h(guid);
		auto f = m_listeners.find(guid);
		if (f != m_listeners.end())
		{
			f->second->emplace(h.getEventListenerId(), listener);
		}
		else
		{
			m_listeners.emplace(guid, std::make_unique<EventListenerMap>());
			m_listeners[guid]->emplace(h.getEventListenerId(), listener);
		}
	
		return h;
	}
}

#endif // EventManager_h__
